* Change the directory to the top one for this project
set more off
capture cd "C:\Users\Benjamin Allen\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"
capture cd "C:\Users\balle\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"
capture cd "~\Loewen-RubensonLab Dropbox\LR Research Lab - Shared\Papers\Support for Algorithmic Governance"


use "data/EPIS_data recoded and labeled.dta", clear


********************************************************************************
* Graphing algorithmic governance acceptibility
********************************************************************************

*------------------------------------------------------------------------------*
* All EPIS data together
*------------------------------------------------------------------------------*

gen alg_govt_acceptable_1_bi_pct = alg_govt_acceptable_1_bi * 100
gen alg_govt_acceptable_2_bi_pct = alg_govt_acceptable_2_bi * 100
gen alg_govt_acceptable_3_bi_pct = alg_govt_acceptable_3_bi * 100
gen alg_govt_acceptable_4_bi_pct = alg_govt_acceptable_4_bi * 100
gen alg_govt_acceptable_5_bi_pct = alg_govt_acceptable_5_bi * 100
gen alg_govt_acceptable_6_bi_pct = alg_govt_acceptable_6_bi * 100
gen alg_govt_acceptable_7_bi_pct = alg_govt_acceptable_7_bi * 100
gen alg_govt_acceptable_8_bi_pct = alg_govt_acceptable_8_bi * 100

sum alg_govt_acceptable_1_bi_pct ///
	alg_govt_acceptable_2_bi_pct ///
	alg_govt_acceptable_3_bi_pct ///
	alg_govt_acceptable_4_bi_pct ///
	alg_govt_acceptable_5_bi_pct ///
	alg_govt_acceptable_6_bi_pct ///
	alg_govt_acceptable_7_bi_pct ///
	alg_govt_acceptable_8_bi_pct
	
count
local sample_count = r(N)

* Categories 
graph bar (mean) ///
	alg_govt_acceptable_1_bi_pct ///
	alg_govt_acceptable_2_bi_pct ///
	alg_govt_acceptable_3_bi_pct ///
	alg_govt_acceptable_4_bi_pct ///
	alg_govt_acceptable_5_bi_pct ///
	alg_govt_acceptable_6_bi_pct ///
	alg_govt_acceptable_7_bi_pct ///
	alg_govt_acceptable_8_bi_pct ///
	, ///
	///percentages ///
	title("How acceptable are the following reasons for governments to""use algorithms and AI to make decisions?", size(medsmall))  ///
	subtitle(`"Percent selected "Acceptable reason to use an algorithm""' "EPIS countries combined", size(small)) ///
	ytitle("Percent") ///
	yline(50) ///
	bar(1, color(eltblue*0.7)) bar(2, color(eltblue)) bar(3, color(ebblue*0.7)) bar(4, color(ebblue)) bar(5, color(edkblue*0.7)) bar(6, color(edkblue)) bar(7, color(eltgreen)) bar(8, color(teal)) ///
	ylabel(0(10)100, angle(0) labsize(vsmall))  ///
	legend(order( /// I've put these even-then-odd, to make the result easier to read as two columns
		1 "To reduce the time required to make decisions" ///
		5 "To reduce fraud against the government." ///
		2 "To make decisions which will be a better use of""government money." ///
		6 `"To make decisions which are more consistent"'`"and less “random”."' ///
		3 "To make sure decisions are not influenced by""factors like a program recipient's gender,""ethnicity, or wealth." ///
		7 "To reduce the number of bureaucrats/government""officials." ///
		4 "To make sure decisions are not influenced by""officials’ biases." ///
		8 "To reduce the costs of government." ///
		) size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)  ///
	note("N = `sample_count'", size(vsmall))  ///
	blabel(bar, format(%4.1f) size(medsmall))
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
graph export "analysis/graphs/EPIS/acceptance for each reason/acceptance for each reason All EPIS data.png", width(2000) replace

*------------------------------------------------------------------------------*
* For each country
*------------------------------------------------------------------------------*

foreach country_recoded in 	"Austria" "Belgium (FR)" "Belgium (NL)" "Denmark" "Finland" "France" "Germany" "Greece" "Ireland" "Italy" "Netherlands" "Norway" "Portugal" "Spain" "Sweden" "UK" {

	preserve

	keep if country_recoded == "`country_recoded'":country_recoded
	
	count
	local sample_count = r(N)

	* Categories 
	graph bar (mean) ///
		alg_govt_acceptable_1_bi_pct ///
		alg_govt_acceptable_2_bi_pct ///
		alg_govt_acceptable_3_bi_pct ///
		alg_govt_acceptable_4_bi_pct ///
		alg_govt_acceptable_5_bi_pct ///
		alg_govt_acceptable_6_bi_pct ///
		alg_govt_acceptable_7_bi_pct ///
		alg_govt_acceptable_8_bi_pct ///
		, ///
		///percentages ///
		title("How acceptable are the following reasons for governments to""use algorithms and AI to make decisions?", size(medsmall))  ///
		subtitle(`"Percent selected "Acceptable reason to use an algorithm""' "`country_recoded'", size(small)) ///
		ytitle("Percent") ///
		yline(50) ///
		bar(1, color(eltblue*0.7)) bar(2, color(eltblue)) bar(3, color(ebblue*0.7)) bar(4, color(ebblue)) bar(5, color(edkblue*0.7)) bar(6, color(edkblue)) bar(7, color(eltgreen)) bar(8, color(teal)) ///
		ylabel(0(10)100, angle(0) labsize(vsmall))  ///
		legend(order( /// I've put these even-then-odd, to make the result easier to read as two columns
			1 "To reduce the time required to make decisions" ///
			5 "To reduce fraud against the government." ///
			2 "To make decisions which will be a better use of""government money." ///
			6 `"To make decisions which are more consistent"'`"and less “random”."' ///
			3 "To make sure decisions are not influenced by""factors like a program recipient's gender,""ethnicity, or wealth." ///
			7 "To reduce the number of bureaucrats/government""officials." ///
			4 "To make sure decisions are not influenced by""officials’ biases." ///
			8 "To reduce the costs of government." ///
			) size(vsmall)) ///
		graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)  ///
		note("N = `sample_count'", size(vsmall))  ///
		blabel(bar, format(%4.1f) size(medsmall))
	local nb=`.Graph.plotregion1.barlabels.arrnels'
	forval i=1/`nb' {
	  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
	  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
	}
	.Graph.drawgraph
	graph export "analysis/graphs/EPIS/acceptance for each reason/acceptance for each reason `country_recoded'.png", width(2000) replace

	restore
}

********************************************************************************
* Algorithmic governance acceptibility scale
********************************************************************************

* First, check scale features
factor alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_5_ord alg_govt_acceptable_6_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord, pcf factors(4)  mineigen(0)


* Binary version
factor alg_govt_acceptable_1_bi alg_govt_acceptable_2_bi alg_govt_acceptable_3_bi alg_govt_acceptable_4_bi alg_govt_acceptable_5_bi alg_govt_acceptable_6_bi alg_govt_acceptable_7_bi alg_govt_acceptable_8_bi, pcf factors(4)  mineigen(0)


alpha alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_5_ord alg_govt_acceptable_6_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord

*sub-scales
* Efficiency
alpha alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_5_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord

* Fairness
alpha alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_6_ord



*------------------------------------------------------------------------------*
* Create scales - ordinal version
*------------------------------------------------------------------------------*

egen alg_govt_acceptable_scale_ord = rowtotal(alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_5_ord alg_govt_acceptable_6_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord)

tab alg_govt_acceptable_scale_ord

* Graphing policy activism on its own
graph twoway (hist alg_govt_acceptable_scale_ord , percent bin(20) fcolor(ebblue*0.5) lcolor(ebblue*0.6)) ///
	, title("Acceptance of algorithmic governance", size(medsmall)) ///
	subtitle("Summed score across 8 reasons""EPIS countries combined", size(small)) ///
	xtitle("Exposure score", size(small))  ///
	xlabel(-8(1)8, labsize(small)) ///
	ylabel(0(5)30, labsize(medsmall) angle(0)) ///
	note( ///
	"Summed score:" ///
	"-1 = Reason is unacceptable" ///
	"0 = Not sure" ///
	"1 = Reason is acceptable" ///
	, size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
graph export "analysis/graphs/EPIS/number of reasons accepted/hist of alg_govt_acceptable_scale_ord all EPIS data.png", width(3000) replace


graph bar (percent) , over(alg_govt_acceptable_scale_ord) ///
	title("Acceptance of algorithmic governance", size(medium)) ///
	subtitle("Summed score across 8 reasons""EPIS countries combined", size(small)) ///
	ytitle("Percent", size(small)) ///
	ylab(0(10)40, angle(0) labsize(small)) ///
	note( ///
	"Summed score:" ///
	"-1 = Reason is unacceptable" ///
	"0 = Not sure" ///
	"1 = Reason is acceptable" ///
	, size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) ///
	bar(1, lcolor(ebblue*0.9) fcolor(eltblue%80)) ///
	blabel(bar, format(%4.1f) size(small))	
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
graph export "analysis/graphs/EPIS/number of reasons accepted/bargraph of alg_govt_acceptable_scale_ord all EPIS data.png", width(2000) replace

* Rescaling to 0-1
sum alg_govt_acceptable_scale_ord
replace alg_govt_acceptable_scale_ord = (alg_govt_acceptable_scale_ord -r(min)) / (r(max)-r(min))
sum alg_govt_acceptable_scale_ord
lab var alg_govt_acceptable_scale_ord "Ordered acceptance of algorithmic governance (scaled 0-1)"

*---------------------------------------*
* Sub-scales
*---------------------------------------*

* Efficiency
egen alg_govt_acc_eff_scale_ord = rowtotal( alg_govt_acceptable_1_ord alg_govt_acceptable_2_ord alg_govt_acceptable_5_ord alg_govt_acceptable_7_ord alg_govt_acceptable_8_ord)

* Fairness
egen alg_govt_acc_fair_scale_ord = rowtotal( alg_govt_acceptable_3_ord alg_govt_acceptable_4_ord alg_govt_acceptable_6_ord) 

graph twoway ///
	(scatter alg_govt_acc_fair_scale_ord alg_govt_acc_eff_scale_ord , jitter(6) mcolor(ebblue%25) msize(tiny)) ///
	(lfit alg_govt_acc_fair_scale_ord alg_govt_acc_eff_scale_ord ) ///
	, ///
	title("Average score of reasons", size(medsmall)) ///
	subtitle("Comparing dimensions""EPIS countries combined", size(small)) ///
	ytitle("Fairness") ///
	xtitle("Efficiency") ///
	ylabel(-3(1)3) ///
	xlabel(-5(1)5) ///
	legend(off) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
graph export "analysis/graphs/EPIS/scatterplots/scatterplot of alg_govt_acc_fair_scale_ord and alg_govt_acc_eff_scale_ord all EPIS data.png", width(3000) replace

* Rescaling to 0-1

* Efficiency
sum alg_govt_acc_eff_scale_ord
replace alg_govt_acc_eff_scale_ord = (alg_govt_acc_eff_scale_ord -r(min)) / (r(max)-r(min))
sum alg_govt_acc_eff_scale_ord
lab var alg_govt_acc_eff_scale_ord "Ordered acceptance of algorithmic governance efficiency items (scaled 0-1)"

* Fairness
sum alg_govt_acc_fair_scale_ord
replace alg_govt_acc_fair_scale_ord = (alg_govt_acc_fair_scale_ord -r(min)) / (r(max)-r(min))
sum alg_govt_acc_fair_scale_ord
lab var alg_govt_acc_fair_scale_ord "Ordered acceptance of algorithmic governance fairness items (scaled 0-1)"

*------------------------------------------------------------------------------*
* Create scales - binary version
*------------------------------------------------------------------------------*

egen alg_govt_acceptable_scale_bi = rowtotal(alg_govt_acceptable_1_bi alg_govt_acceptable_2_bi alg_govt_acceptable_3_bi alg_govt_acceptable_4_bi alg_govt_acceptable_5_bi alg_govt_acceptable_6_bi alg_govt_acceptable_7_bi alg_govt_acceptable_8_bi)

tab alg_govt_acceptable_scale_bi

* 67.04% find at least half of the reasons accetpable
* 23.85% find all of the reasons acceptable

/* OLD WAY OF GRAPHING IT - the bars don't look quite right this way, and we can't label them

* Graphing policy activism on its own
graph twoway (hist alg_govt_acceptable_scale_bi , percent  fcolor(ebblue*0.5) lcolor(ebblue*0.6)) ///
	, title("Acceptance of algorithmic governance", size(medsmall)) ///
	subtitle("Total accepted among 8 reasons", size(small)) ///
	xtitle("Number of reasons found acceptable", size(small))  ///
	xlabel(0(1)8, labsize(small)) ///
	ylabel(0(5)30, labsize(medsmall) angle(0)) ///
	note( ///
	"Average score of reasons:" ///
	"0 = Unacceptable or Not sure" ///
	"1 = Reason is acceptable" ///
	, size(vsmall)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
graph export "analysis/graphs/number of reasons accepted/hist of alg_govt_acceptable_scale_bi.png", width(3000) replace

*/

graph bar (percent) , over(alg_govt_acceptable_scale_bi) ///
	title("Acceptance of algorithmic governance", size(medium)) ///
	subtitle("Number of reasons found acceptable""EPIS countries combined", size(small)) ///
	ytitle("Percent", size(small)) ///
	ylab(0(10)40, angle(0) labsize(small)) ///
	///note("Weighted by province, age group, and gender", size(vsmall))  ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white) ///
	bar(1, lcolor(ebblue*0.9) fcolor(eltblue%80)) ///
	blabel(bar, format(%4.1f) size(medsmall))	
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
graph export "analysis/graphs/EPIS/number of reasons accepted/bargraph of alg_govt_acceptable_scale_bi all EPIS data.png", width(2000) replace

* Fairness
sum alg_govt_acceptable_scale_bi
replace alg_govt_acceptable_scale_bi = (alg_govt_acceptable_scale_bi -r(min)) / (r(max)-r(min))
sum alg_govt_acceptable_scale_bi
lab var alg_govt_acceptable_scale_bi "Binary acceptance of algorithmic governance (scaled 0-1)"

*---------------------------------------*
* Sub-scales
*---------------------------------------*

* Efficiency
egen alg_govt_acc_eff_scale_bi = rowtotal( alg_govt_acceptable_1_bi alg_govt_acceptable_2_bi alg_govt_acceptable_5_bi alg_govt_acceptable_7_bi alg_govt_acceptable_8_bi)

* Fairness
egen alg_govt_acc_fair_scale_bi = rowtotal( alg_govt_acceptable_3_bi alg_govt_acceptable_4_bi alg_govt_acceptable_6_bi) 

tab alg_govt_acc_fair_scale_bi alg_govt_acc_eff_scale_bi , cell

graph twoway ///
	(scatter alg_govt_acc_fair_scale_bi alg_govt_acc_eff_scale_bi , jitter(6) mcolor(ebblue%25) msize(tiny)) ///
	(lfit alg_govt_acc_fair_scale_bi alg_govt_acc_eff_scale_bi ) ///
	, ///
	title("Number of reasons accepted", size(medsmall)) ///
	subtitle("Comparing dimensions""EPIS countries combined", size(small)) ///
	ytitle("Fairness") ///
	xtitle("Efficiency") ///
	ylabel(0(1)3) ///
	xlabel(0(1)5) ///
	legend(off) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)
graph export "analysis/graphs/EPIS/scatterplots/scatterplot of alg_govt_acc_fair_scale_bi and alg_govt_acc_eff_scale_bi all EPIS data.png", width(3000) replace


*------------------------------------------------------------------------------*
* Algorithmic governance acceptibility subscale categories
*------------------------------------------------------------------------------*

* Efficiency
tab alg_govt_acc_eff_scale_bi

gen alg_gov_sup_eff = 0
replace alg_gov_sup_eff = 1 if alg_govt_acc_eff_scale_bi >= 3

lab var alg_gov_sup_eff "Acceptance for a majority of efficiency reasons"
lab def alg_gov_sup_eff 0 "Does not accept majority" 1 "Accepts majority"
lab val alg_gov_sup_eff alg_gov_sup_eff

* Fairness
tab alg_govt_acc_fair_scale_bi

gen alg_gov_sup_fair = 0
replace alg_gov_sup_fair = 1 if alg_govt_acc_fair_scale_bi >= 2

lab var alg_gov_sup_fair "Support for a majority of fairness reasons"
lab def alg_gov_sup_fair 0 "Does not accept majority" 1 "Accepts majority"
lab val alg_gov_sup_fair alg_gov_sup_fair

* Twoway table
tab alg_gov_sup_eff alg_gov_sup_fair, cell

*------------------------------------------------------------------------------*
* Rescaling to 0-1
*------------------------------------------------------------------------------*

* Efficiency
sum alg_govt_acc_eff_scale_bi
replace alg_govt_acc_eff_scale_bi = (alg_govt_acc_eff_scale_bi -r(min)) / (r(max)-r(min))
lab var alg_govt_acc_eff_scale_bi "Binary acceptance of algorithmic governance efficiency items (scaled 0-1)"
sum alg_govt_acc_eff_scale_bi

* Fairness
sum alg_govt_acc_fair_scale_bi
replace alg_govt_acc_fair_scale_bi = (alg_govt_acc_fair_scale_bi -r(min)) / (r(max)-r(min))
sum alg_govt_acc_fair_scale_bi
lab var alg_govt_acc_fair_scale_bi "Binary acceptance of algorithmic governance fairness items (scaled 0-1)"

/* ALTERNATE WAY OF GRAPHING IT
preserve

collapse (count) N = gc, by(cps19_surveyday)

graph twoway (bar N cps19_surveyday , lcolor(ebblue*0.9) fcolor(eltblue%80)) ///
	, ///
	title("Acceptance of algorithmic governance", size(medium)) ///
	subtitle("CES 2019 Campaign Period Survey", size(small)) ///
	ytitle("Number of responses", size(small)) ///
	xtitle("Survey day", size(small)) ///
	xlab(1(2)39, valuelabel angle(45) labsize(small)) ///
	ylab(0(250)2000, angle(0) labsize(small)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) bgcolor(white)

restore
*/



********************************************************************************
* Creating anti-politics scale
********************************************************************************

*------------------------------------------------------------------------------*
* Populism
*------------------------------------------------------------------------------*

* Creating combined "populism" scale, and anti-elitism and strong leader subscales
* This is a bit awkward, as there are only 3 of the 7 usual items in the EPIS data

codebook populism_2 populism_4 populism_7

* Raw populism. Only the last item (populism_7) is reversed
gen POP2 = (populism_2 -1)/4
gen POP4 = (populism_4 -1)/4
gen POP7 = (populism_7*-1 +5)/4

sum POP2 POP4 POP7

egen populism_scale = rowmean(POP2 POP4 POP7)
lab var populism_scale "Populism (0-1)"

sum populism_scale

alpha POP2 POP4 POP7

factor POP2 POP4 POP7


* Anti-elitism subscale
egen antielitism = rowmean(POP2 POP7)
lab var antielitism "Anti-elitism (0-1)"

sum antielitism

sum POP2 POP7
alpha POP2 POP7
factor POP2 POP7


* Strong leader subscale
egen strongleader = rowmean(POP4)
lab var strongleader "Strong Leader (0-1)"

sum strongleader

sum POP4
*alpha POP4
*factor POP4

********************************************************************************
* Creating nativism scale
********************************************************************************
/*
codebook nativism_1 nativism_2 nativism_3 nativism_4 nativism_5 nativism_6

* Only the 4th Nativism item is reversed
gen NAT1 = (nativism_1 -1)/4
gen NAT2 = (nativism_2 -1)/4
gen NAT3 = (nativism_3 -1)/4
gen NAT4 = (nativism_4*-1 +5)/4
gen NAT5 = (nativism_5 -1)/4
gen NAT6 = (nativism_6 -1)/4

sum NAT1-NAT6

egen nativism_scale = rowmean(NAT1-NAT6)
lab var nativism_scale "Nativism (0-1)"

sum nativism_scale

alpha NAT1-NAT6
* Wow, that alpha is so much stronger

factor NAT1-NAT6
*/
********************************************************************************
* Rescaling other variables to 0-1
********************************************************************************

sum age_in_years
gen age_in_years_rescaled = (age_in_years -r(min)) / (r(max)-r(min))
sum age_in_years_rescaled
lab var age_in_years_rescaled "Age in years (rescaled 0-1)"

sum subjective_income
gen subjective_income_rescaled = (subjective_income -r(min)) / (r(max)-r(min))
sum subjective_income_rescaled
lab var subjective_income_rescaled "Subjective income (rescaled 0-1)"

sum education_cat
gen education_cat_rescaled = (education_cat -r(min)) / (r(max)-r(min))
sum education_cat_rescaled
lab var education_cat_rescaled "Education category (rescaled 0-1)"

sum lr_scale
gen lr_scale_rescaled = (lr_scale -r(min)) / (r(max)-r(min))
sum lr_scale_rescaled
lab var lr_scale_rescaled "Left-Right scale (rescaled 0-1)"

/*

*standardizing the variable to 0-1
sum combined_jobloss_scale
replace combined_jobloss_scale = (combined_jobloss_scale-r(min))/ (r(max)-r(min))
sum combined_jobloss_scale

lab var combined_jobloss_scale "Personal and prosocial job loss expectations (-8/+8)"

*/


save "data/EPIS_data recoded and labeled with scales.dta", replace
